package com.ztf.gui;

import com.ztf.bean.DataTransfer;
import com.ztf.bean.Message;
import com.ztf.bean.User;
import com.ztf.customer.Customer;


import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.SystemColor;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.*;

public class ChatRoom extends JFrame {

	private JPanel contentPane;
	public static ChatRoom chatRoom;
	public static User userNow;
	//最大接待人数
	public JLabel label2;

	//保存客服与每一个客户的聊天记录  User 是客户
	public static Map<User,List<Message>> fr = new HashMap<>();

	//聊天消息
	public JList list_1;

	//客户列表
	public JList list;

	public JLabel lblNewLabel;

	//发送对象
	public User isSelected;

	//今日接待人数
	public int receivedPerson;
	public JLabel label4;

	//当前等待人数
	public JLabel label6;
	//正在输入
	public JLabel label7;

	boolean isInput = true;

	/**
	 * Create the frame.
	 */
	public ChatRoom() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 50, 1000, 700);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		//好友列表
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(19, 38, 214, 622);
		contentPane.add(scrollPane);
		list = new JList();
		list.setCellRenderer(new ImageCellRender());
		scrollPane.setViewportView(list);
		
		//聊天记录
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(245, 38, 749, 534);
		contentPane.add(scrollPane_1);
		list_1 = new JList();
		list_1.setCellRenderer(new MsgCellRender());
		scrollPane_1.setViewportView(list_1);
		
		String file = "customerService/head/4.jpg";

		
//		int height = 40;
//	    Point p = new Point();
//	    p.setLocation(0, 20 * height);
//	    scrollPane.getViewport().setViewPosition(p);
		
		//滚动到最后
		int height = 40;
	    Point p = new Point();
	    p.setLocation(0, 20 * height);
	    scrollPane_1.getViewport().setViewPosition(p);
		
		JTextArea textArea = new JTextArea();
		textArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				//回车
				if(e.getKeyCode()==10) {
					if (isSelected==null){
						JOptionPane.showMessageDialog(null,"发送对象不能为空");
						return;
					}
					String text = textArea.getText();
					long time = System.currentTimeMillis();
					User from  = new User(userNow.getName(),userNow.getHead(),userNow.getIdentity(),userNow.getMaxServerPerson());
					User to = new User(isSelected.getName(),isSelected.getHead(),isSelected.getIdentity(),isSelected.getMaxServerPerson());
					Message message = new Message(from,to,time,text);
					Customer.send(new DataTransfer("msg",message));
					textArea.setText("");
					isInput = true;
				}
			}
		});
		textArea.setBounds(245, 582, 649, 78);
		contentPane.add(textArea);


		textArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (isInput && isSelected!=null && e.getKeyCode()!=10){
					Customer.send(new DataTransfer("inputing",isSelected));
					isInput = false;
				}
			}
		});
		
		JLabel label = new JLabel("发送给：");
		label.setBounds(906, 582, 61, 16);
		contentPane.add(label);
		
		lblNewLabel = new JLabel("");
		//lblNewLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		lblNewLabel.setForeground(new Color(30, 144, 255));
		lblNewLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(905, 603, 88, 25);
		contentPane.add(lblNewLabel);
		
		JButton btnNewButton = new JButton("发送");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("dddd");
			}
		});
		btnNewButton.setBounds(906, 631, 88, 29);
		contentPane.add(btnNewButton);
		
		JLabel label_1 = new JLabel(userNow.getName());
		label_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});

		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setForeground(new Color(30, 144, 255));
		label_1.setBounds(741, 10, 128, 16);
		contentPane.add(label_1);
		
		JLabel lblNewLabel_1 = new JLabel("，欢迎您");
		lblNewLabel_1.setBounds(881, 10, 61, 16);
		contentPane.add(lblNewLabel_1);
		
		//个人头像
		ImagePanel imagePanel = new ImagePanel(null);
		imagePanel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});

		imagePanel.setBounds(954, 0, 40, 40);
		BufferedImage head=null;
		try {
			head = ImageIO.read(new ByteArrayInputStream(userNow.getHead()));
			imagePanel.setImage(head);
			contentPane.add(imagePanel);
			
			JButton button = new JButton("退出");
			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {

				}
			});
			button.setBounds(19, 5, 98, 29);
			contentPane.add(button);
			contentPane.repaint();
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		label7 = new JLabel("");
		label7.setBounds(200,5,100,29);
		contentPane.add(label7);



		JLabel label5 = new JLabel("当前等待人数:");
		label5.setBounds(300,5,100,29);
		contentPane.add(label5);

		label6 = new JLabel(0+"");
		label6.setBounds(400,5,15,29);
		contentPane.add(label6);


		JLabel label3 = new JLabel("今日接待人数:");
		label3.setBounds(500,5,100,29);
		contentPane.add(label3);

		label4 = new JLabel(receivedPerson+"");
		label4.setBounds(600,5,15,29);
		contentPane.add(label4);


		JLabel label1 = new JLabel("同时接纳人数:");
		label1.setBounds(643,5,120,29);
		contentPane.add(label1);

		label2 = new JLabel(userNow.getMaxServerPerson()+"");
		label2.setBounds(780,5,30,29);
		contentPane.add(label2);
		label2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		label2.addMouseListener(new MouseInputAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				String input = JOptionPane.showInputDialog(null, "请输入");
				if (input==null){
					JOptionPane.showMessageDialog(null,"接待人数不能为空");
					return;
				}

				int person;
				try {
					person = Integer.parseInt(input);
				}catch (Exception ee){
					JOptionPane.showMessageDialog(null,"接待人数只能是整数");
					return;
				}
				if (person<=0){
					JOptionPane.showMessageDialog(null,"接待人数只能是大于0的整数");
					return;
				}
				if (person<fr.size()){
					JOptionPane.showMessageDialog(null,"不能低于当前正在接待的人数");
					return;
				}
				label2.setText(input);
				userNow.setMaxServerPerson(person);
				Customer.send(new DataTransfer("updateMaxPerson",person));
			}
		});



	}
}
